package com.jeecg.p3.base.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *
 * @ClassName: RandomUtil
 * @Description: 生成随机数数组类
 * @author: zhangxiao
 * @date: 2020年10月21日 下午6:16:43
 */
public class RandomUtil {
    /**
     *
     * @Title: getRandomByLength
     * @Description: 根据传入长度生成相应长度随机数
     * @param length
     * @return 随机数
     * @author zhangxiao
     * @return: String
     */
    public static String getRandomByLength(Integer length) {
        Random rand = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i <= length; i++) {
            int randNum = rand.nextInt(9) + 1;
            String num = randNum + "";
            sb = sb.append(num);
        }
        String random = String.valueOf(sb);
        return random;
    }


    public static String getAppId() {
        //格式化当前时间
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = sfDate.format(new Date());
        //得到17位时间如：20170411094039080
        //System.out.println("时间17位：" + strDate);
        //为了防止高并发重复,再获取3个随机数
        String random = getRandom620(3);
        //最后得到20位订单编号。
        System.out.println("订单号20位：" + strDate + random);
        return strDate + random;
    }

    public static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);
            result += randInt;
        }
        return result;
    }
}
